package game.zqll.systems;

import com.artemis.Aspect;
import com.artemis.BaseSystem;
import com.artemis.EntitySubscription;
import com.artemis.utils.IntBag;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.kotcrab.vis.ui.widget.VisLabel;
import game.zqll.components.DropComp;

public class ModGuiSystem extends BaseSystem {

    private GuiSystem guiSystem;
    private VisLabel infoLabel;
    private VisLabel countdownLabel;
    private VisLabel restartLabel;
    private EntitySubscription subscription;
    private StringBuilder sb = new StringBuilder();
    private int score = 0;

    private boolean hasAddAction = false;
    private boolean gameOver = false;
    private float initCountdown = 10;
    private float countdown = 60;

    @Override
    protected void initialize() {
        Stage stage = guiSystem.getStage();
        stage.addActor(new Table() {{
            setFillParent(true);
            defaults().pad(5).expand();
            add(infoLabel = new VisLabel("")).top().left();
        }});
        stage.addActor(new Table() {{
            setFillParent(true);
            defaults().pad(5).expand();
            add(countdownLabel = new VisLabel("")).center().top();
        }});
        stage.addActor(new Table() {{
            setFillParent(true);
            defaults().pad(5).expand();
            add(restartLabel = new VisLabel("press any key to restart") {{
                addAction(Actions.forever(Actions.sequence(Actions.fadeOut(0.74f), Actions.fadeIn(0.79f))));
            }}).center();
        }});

        subscription = world.getAspectSubscriptionManager().get(Aspect.all());
        this.restart();
    }

    @Override
    protected void processSystem() {
        infoLabel.setText(updateInfo());
        countdownLabel.setText(updateCountdown());
        if (this.gameOver && Gdx.input.isKeyJustPressed(Input.Keys.ANY_KEY)) {
            this.restart();
        }
    }

    private StringBuilder updateInfo() {
        sb.setLength(0);
        sb.append("score: ").append(score);
        sb.append("\n");
        sb.append("Entity Count: ").append(subscription.getEntities().size());
        sb.append("\n");
        sb.append("FPS: ").append(Gdx.graphics.getFramesPerSecond());
        return sb;
    }

    public void restart() {
        this.countdown = initCountdown;
        this.hasAddAction = false;
        this.score = 0;
        this.gameOver = false;
        IntBag entities = world.getAspectSubscriptionManager().get(Aspect.all(DropComp.class)).getEntities();
        for (int i = 0; i < entities.size(); i++) {
            world.delete(entities.get(i));
        }
        countdownLabel.clearActions();
        this.restartLabel.setVisible(false);
    }

    private StringBuilder updateCountdown() {
        if (countdown <= 0f) {
            this.gameOver = true;
            sb.setLength(0);
            sb.append((int) countdown);
            this.restartLabel.setVisible(true);
            this.countdownLabel.clearActions();
            return sb;
        }
        countdown -= world.delta;
        if (countdown <= initCountdown * 0.25f) {
            if (!hasAddAction) {
                countdownLabel.addAction(Actions.forever(Actions.sequence(Actions.fadeOut(0.74f), Actions.fadeIn(0.79f))));
                hasAddAction = true;
            }
        } else {
            countdownLabel.clearActions();
        }
        sb.setLength(0);
        sb.append((int) countdown);
        return sb;
    }

    public void incrScore() {
        if (gameOver) {
            return;
        }
        this.score++;
    }

    public void decrCountdown() {
        this.countdown--;
    }

    public VisLabel getInfoLabel() {
        return infoLabel;
    }
}
